package com.xingdata.jjxc.m.avt;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.enty.CloudEntity;

public class CloudYMM extends AbActivity {

	protected AbHttpUtil mAbHttpUtil = null;
	protected AbRequestParams params = null;
	protected CloudEntity resp = null;

	String url = "http://api.map.baidu.com/telematics/v3/weather";

	String str = "朔州市";

	private String pm = null;
	private String weather_iv = null;
	private String weather_str = null;
	private String weather_temperature = null;
	private String weather_date = null;

	private TextView txt;
	private ImageView iv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cloud_ymm);

		mAbHttpUtil = AbHttpUtil.getInstance(this);
		mAbHttpUtil.setDebug(true);
		mAbHttpUtil.setTimeout(15000);

		txt = (TextView) findViewById(R.id.txt);
		iv = (ImageView) findViewById(R.id.iv);
		showWeather();

	}

	private void showWeather() {
		try {
			String change_str = URLEncoder.encode(str, "utf-8");

			/**
			 * 天气Pm接口
			 */
			String weather_url = "http://api.map.baidu.com/telematics/v3/weather?location="
					+ change_str + "&output=json&ak=6kxjfueX7H8FwkfHY5UtrK4b";

			/**
			 * 道路故障接口
			 */
			// String
			// weather_url="http://api.map.baidu.com/telematics/v3/trafficEvent?location="+change_str+"&output=json&ak=6kxjfueX7H8FwkfHY5UtrK4b";

			Log.i("天气信息链接", weather_url);
			doPost_checkVersion(weather_url);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doPost_checkVersion(String url) {
		resp = null;
		params = new AbRequestParams();
		mAbHttpUtil.get(url, params, new AbStringHttpResponseListener() {
			@Override
			public void onSuccess(int statusCode, String content) {
				resp = JSON.parseObject(content, CloudEntity.class);
				if (("302").equals(resp.getStatus())){
					showToast("网络超时，请稍后重试……");

					return;
				}
			}

			@Override
			public void onFailure(int statusCode, String content,
					Throwable error) {
			}

			@Override
			public void onStart() {
			}

			@Override
			public void onFinish() {
				if (!("302").equals(resp.getStatus()) && resp != null) {
					Log.i("resp", resp + "");
					if (resp.getError() == 0 && (!"".equals(resp.getResults()))) {
						JSONArray arr;
						try {
							arr = new JSONArray(resp.getResults());
							for (int i = 0; i < arr.length(); i++) {
								JSONObject temp = (JSONObject) arr.get(i);
								// PM2.5
								pm = temp.getString("pm25");
								Log.i("pm", pm);
								// 天气显示
								JSONArray weather_index = temp
										.getJSONArray("index");

								// 天气显示
								JSONArray weather_ob = temp
										.getJSONArray("weather_data");
								weather_date = weather_ob.getJSONObject(0)
										.getString("date");
								String wea = weather_date.substring(
										weather_date.length() - 4,
										weather_date.length() - 1);
								Log.i(wea, wea);
								weather_iv = weather_ob.getJSONObject(0)
										.getString("dayPictureUrl");
								weather_str = weather_ob.getJSONObject(0)
										.getString("weather");
								weather_temperature = weather_ob.getJSONObject(
										0).getString("temperature");

								String zs = null;
								for (int j = 0; j < weather_index.length(); j++) {
									JSONObject index = (JSONObject) weather_index
											.get(j);
									String tipt = index.getString("tipt");
									if (tipt.equals("洗车指数")) {
										zs = index.getString("zs");
										continue;
									}
								}
								String str_weather = "洗车指数：" + zs + weather_str
										+ " " + wea + "~" + weather_temperature
										+ " " + " PM2.5:" + pm;
								txt.setText(str_weather);
								Log.i("天气状况", str_weather);

								if (weather_iv.contains("http://")) {
									App.imageLoader.displayImage(weather_iv,
											iv, App.options);
								} else {
									App.imageLoader.displayImage(
											App.SEGMENTSOURCE + weather_iv, iv,
											App.options);
								}

							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					} else {
						showToast("网络超时，请重试");
					}
					removeProgressDialog();
				} else {
					showToast("网络超时，请重试");
					removeProgressDialog();
					return;
				}
			}
		});
	}
}
